from sqlalchemy.ext.declarative import declared_attr
from sqlalchemy.orm import class_mapper
from datetime import datetime
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy import Column, String, Float, JSON, DateTime,INT
import uuid

db = SQLAlchemy()

class SerializerMixin:
    @declared_attr
    def __tablename__(cls):
        return cls.__name__.lower()

    def to_dict(self):
        return {c.key: getattr(self, c.key) for c in class_mapper(self.__class__).columns}

def init_db(app):
    with app.app_context():
        db.create_all()
        count = User.query.filter(User.username=="root").count()
        if (count==0):
            current_datetime = datetime.now()
            info = User(id=str(uuid.uuid4()), username="root",password="123456",email="test@163.com",telephone="13810001000", timestamp=current_datetime)
            db.session.add(info)
            db.session.commit()

class Task(db.Model, SerializerMixin):
    __tablename__ = 'task'
    
    # 使用 UUID 作为主键
    id = Column(String, primary_key=True)
    
    # 其他字段可以根据需要定义
    taskServer = Column(String(255), nullable=False) # 推理/训练服务器地址
    targetType = Column(String(255), nullable=False) # 检测类型
    modelFrame = Column(String(255), nullable=False) # 模型框架
    media = Column(String, nullable=False)  # 视频流
    mediaID = Column(String, nullable=False)
    confidence = Column(Float)  # 置信度
    pid = Column(String) # 预测启动的进程号
    fps = Column(String)
    # 时间
    timestamp = Column(DateTime)

class Alarm(db.Model, SerializerMixin):
    __tablename__ = 'alarm'
    
    # 使用 UUID 作为主键
    id = Column(String, primary_key=True)
    
    # 使用 JSON 来存储列表
    box = Column(JSON)  
    classType = Column(String(255))  # 模型文件名
    confidence = Column(Float)  # 置信度
    file_path = Column(String(1024))  # 图片路径
    target = Column(String)  # base64 编码的字符串
    mediaID = Column(String, nullable=False)
    timestamp = Column(DateTime)

class User(db.Model, SerializerMixin):
    __tablename__ = 'user'
    
    # 使用 UUID 作为主键
    id = Column(String, primary_key=True)
    
    # 其他字段可以根据需要定义
    username = Column(String(80), nullable=False)
    password = Column(String(80), nullable=False)
    telephone = Column(String(80), nullable=True)
    email = Column(String(80), nullable=True)
    timestamp = Column(DateTime)

class Dictionary(db.Model, SerializerMixin):
    __tablename__ = 'dictionary'
    
    # 使用 UUID 作为主键
    id = Column(String, primary_key=True)
    pid = Column(String, nullable=True)
    category = Column(String(80), nullable=False)
    key = Column(String(80), nullable=False)
    value = Column(String(80), nullable=False)
    timestamp = Column(DateTime)

class MediaSource(db.Model, SerializerMixin):
    __tablename__ = 'mediasource'
    
    # 使用 UUID 作为主键
    id = Column(String, primary_key=True)
    name = Column(String(80), nullable=False)
    url = Column(String(255), nullable=False)
    code = Column(String(80), nullable=True)
    width = Column(INT)
    height = Column(INT)
    timestamp = Column(DateTime)



    